﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Baula.Testing.Common
{
    // Idea from http://jamesnewkirk.typepad.com/posts/2008/06/replacing-expec.html
    // Code made by myself :)
    public static class ExceptionAssert
    {
        public delegate void SelfContainedAction();

        public static TException Expect<TException>(SelfContainedAction action)
            where TException : Exception
        {
            try
            {
                action.Invoke();
            }
            catch (Exception xcp)
            {
                Assert.IsInstanceOfType(xcp, typeof(TException));
                return (TException) xcp;
            }
            Assert.Fail(string.Format("An exception of type \"{0}\" was expected but no exception occured.", typeof(TException)));
            return null;
        }

        public static Exception Expect<TResult>(Func<TResult> action)
        {
            try
            {
                action.Invoke();
            }
            catch (Exception xcp)
            {
                return xcp;
            }
            Assert.Fail(string.Format("An exception was expected but no exception occured."));
            return null;
        }
    }
}